package com.bidevalution.controller.task;

import com.bidevalution.business.dto.SectionInfoDto;
import com.bidevalution.business.service.BeDataSyncService;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.google.common.collect.Maps;
import com.qtp.core.common.RemoteService;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.GsonUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 标段数据同步定时任务
 * Created by glodon on 2019-08-06.
 */
@Component
public class SectionDataTask {

    @Value("${ids.url}")
    private String ptIdsUrl;

    @Value("${t9.fileServer.url}")
    private String t9FileServerUrl;

    @Autowired(required = false)
    private BeDataSyncService beDataSyncService;

    @Autowired(required = false)
    private BeSectionInfoMapper beSectionInfoMapper;
    private Logger log = Logger.getLogger(getClass());
    /**
     * 10分钟执行一次
     *
     * @throws Exception
     */
    //@Scheduled(cron = "0 */1 *  * * * ")
    public void syncSectionInfoData() throws Exception {
        Map<String, Object> params = Maps.newHashMap();
        params.put("od", DateUtil.getCurrentDateStr(DateUtil.PT1));
        //params.put("od", "2019-09-07");
        params.put("busiType", "1");
        String sectionViewUrl = ptIdsUrl + "/sectionView.shtml";
        try {
            //调用t9平台接口查询今日开标的标段信息
            String response = RemoteService.getRemoteInfo(sectionViewUrl, params);
            if (StringUtils.isNotBlank(response)) {
                Map<String, Object> resultMap = GsonUtil.jsonToMapWithGson(response);
                if (resultMap != null) {
                    List<Map<String, Object>> sectionsList = (List<Map<String, Object>>) resultMap.get("sections");
                    if (sectionsList != null && sectionsList.size() > 0) {
                        for (Map<String, Object> map : sectionsList) {
                            if (map != null) {
                                String sectionId = (String)map.get("bsID");
                                BeSectionInfo beSectionInfo = beSectionInfoMapper.selectByPrimaryKey(sectionId);
                                //标段数据已存在，且开标时间未变更，则不再进行同步
                                if(beSectionInfo !=null && beSectionInfo.getBidOpenTime() !=null
                                        && beSectionInfo.getBidOpenTime().equals(DateUtil.stringToDate((String)map.get("openTime"), DateUtil.PT2))){
                                    continue;
                                }
                                try {
                                    //数据转换
                                    SectionInfoDto sectionInfoDto = transferSectionInfo(map);
                                    //数据同步
                                    beDataSyncService.syncSectionInfo(sectionInfoDto);
                                } catch (Exception e) {
                                    log.error("标段["+(String) map.get("bsName")+"]导入失败，", e);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 从t9平台获取数据转换
     *
     * @return
     * @throws Exception
     */
    private SectionInfoDto transferSectionInfo(Map<String, Object> map) throws Exception {
        SectionInfoDto sectionInfoDto = new SectionInfoDto();
        BeSectionInfo beSectionInfo = new BeSectionInfo();
        String sectionId = (String) map.get("bsID");
        beSectionInfo.setSeqId(sectionId);
        beSectionInfo.setSectionName((String) map.get("bsName"));
        beSectionInfo.setBidOpenTime(DateUtil.stringToDate((String) map.get("openTime"), DateUtil.PT2));
        beSectionInfo.setAgentOrgCode((String) map.get("zbdlOrgCode"));
        beSectionInfo.setBidOpenAddress((String) map.get("openRoom"));
        beSectionInfo.setTenderee((String) map.get("zbdwName"));
        beSectionInfo.setAgentName((String) map.get("zbdlName"));
        beSectionInfo.setSectionCode((String) map.get("sectiCode"));
        beSectionInfo.setBidType(map.get("tenderType") == null ? "" : map.get("tenderType").toString());
        beSectionInfo.setCreateTime(DateUtil.now());
        beSectionInfo.setUpdateTime(DateUtil.now());
        sectionInfoDto.setBeSectionInfo(beSectionInfo);
        String fileMD5 = (String) map.get("fileMD5");
        //String fileMD5 = "EBCE91763888426069EA0C75A7DF35B7";
        sectionInfoDto.setFileId(fileMD5);
        return sectionInfoDto;
    }

}
